//
//  NewFeatureViewController.swift
//  SinaBlog
//
//  Created by 有何不可 on 16/1/4.
//  Copyright © 2016年 有何不可. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let count: Int = 4

class NewFeatureViewController: UICollectionViewController {
    
    let flowLayout = UICollectionViewFlowLayout()
    
    init() {
        
        super.init(collectionViewLayout: flowLayout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        setUI()
    }
    
    func setUI() {
        
        flowLayout.itemSize = UIScreen.mainScreen().bounds.size
        
        flowLayout.scrollDirection = .Horizontal
        
        flowLayout.minimumInteritemSpacing = 0
        
        flowLayout.minimumLineSpacing = 0
        
        collectionView?.showsHorizontalScrollIndicator = false
        
        collectionView?.showsVerticalScrollIndicator = false
        
        collectionView?.pagingEnabled = true
        
        collectionView?.bounces = false
        
        collectionView?.registerClass(NewFeatureCollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
}

//MARK: --数据源代理方法collectionView

extension NewFeatureViewController {
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return count
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCollectionCell
        
//        cell.backgroundColor = (indexPath.item % 2 == 0) ? UIColor.redColor() : UIColor.whiteColor()
        
        cell.index = indexPath.item
        
        return cell
    }
}

//MARK: --自定义Cell
class NewFeatureCollectionCell: UICollectionViewCell {
    
    var index: Int = 0 {
        
        didSet {
            
            imgCell.image = UIImage(named: "new_feature_\(index + 1)")
            
            if index == 3 {
                
                enterBtn.hidden = false
            } else {
                
                enterBtn.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUI() {
        
        addSubview(imgCell)
        addSubview(enterBtn)
        
        imgCell.frame = bounds
        
        enterBtn.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: enterBtn, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: enterBtn, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: -150))
        
        
    }
    
    private lazy var imgCell: UIImageView = {
        
        let imgView = UIImageView()
        
        return imgView
        }()
    
    
    private lazy var enterBtn: UIButton = {
        
        let btn = UIButton(type: .Custom)
        
        btn.setTitle("立即进入", forState: .Normal)
        
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        
        btn.sizeToFit()
        
        btn.addTarget(self, action: "enterBtnClick", forControlEvents: .TouchUpInside)
        
        return btn
        }()
    
    @objc private func enterBtnClick() {
        
        NSNotificationCenter.defaultCenter().postNotificationName(key, object: nil)
        
    }
}
